package com.cjj;


import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

/**
 * Created by cjj on 2016/2/22.
 */
public class SunLineView extends Component implements Component.DrawTask, Component.EstimateSizeListener {

    private static final String Tag = SunLineView.class.getSimpleName();

    private int mHeight, mWidth;
    private Paint mLinePaint;//线的画笔
    private int mLineLeft, mLineTop;// 线的左、上位置
    private int mLineHeight;  // 短线长度
    private int mLineWidth;//线宽度
    private int mFixLineHeight;
    private int mLineBottom;
    private int mSunRadius;
    private Rect debugRect;
    private RectFloat mouthRect;
    //    private DrawFilter mDrawFilter;
    private int mLineColor;
    private int mLineLevel;

    public SunLineView(Context context) {
        this(context, null);
    }

    public SunLineView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public SunLineView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        setLayoutRefreshedListener(new LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                onSizeChanged(component.getWidth(), component.getHeight());
            }
        });
        setEstimateSizeListener(this);
        addDrawTask(this);
    }

    private void init() {
        HiLog.info(Contants.LABEL, "init");
        mLineWidth = changeDp(1);
        mLineHeight = changeDp(3);
        mFixLineHeight = changeDp(6);
        mSunRadius = changeDp(12);
        mLineColor = Color.RED.getValue();
        mLineLevel = 30;

        //线的配置
        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(new Color(mLineColor));
        mLinePaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        // 设置画笔宽度
        mLinePaint.setStrokeWidth(mLineWidth);
//        mDrawFilter = new Paint(0, Paint.ANTI_ALIAS_FLAG
//                | Paint.FILTER_BITMAP_FLAG);
        debugRect = new Rect();
        mouthRect = new RectFloat();
    }

    public void setLineColor(int lineColor) {
        mLineColor = lineColor;
        invalidate();
    }

    public void setLineWidth(int lineWidth) {
        mLineWidth = changeDp(lineWidth);
        invalidate();
    }

    public void setLineHeight(int lineHeight) {
        mLineHeight = changeDp(lineHeight);
        mFixLineHeight = mLineHeight * 2;
        invalidate();
    }

    /**
     * 设置太阳半径
     *
     * @param sunRadius 半径
     */
    public void setSunRadius(int sunRadius) {
        mSunRadius = changeDp(sunRadius);
        invalidate();
    }

    public void setLineLevel(int level) {
        mLineLevel = level;
        invalidate();
    }

    protected void onSizeChanged(int w, int h) {
//        HiLog.info(Contants.label, "w---->" + w + "  -------  h----->" + h);
        System.out.println("720--------onSizeChanged");
//        mWidth = w;
//        mHeight = h;

        debugRect.left = mWidth / 2 - mSunRadius - mFixLineHeight;
        debugRect.right = mWidth / 2 + mSunRadius + mFixLineHeight;
        debugRect.top = mHeight / 2 - mSunRadius - mFixLineHeight;
        debugRect.bottom = mHeight / 2 + mSunRadius + mFixLineHeight;

        mouthRect.left = mWidth / 2 - mSunRadius / 2;
        mouthRect.right = mWidth / 2 + mSunRadius / 2;
        mouthRect.top = mHeight / 2 - mSunRadius / 2;
        mouthRect.bottom = mHeight / 2 + mSunRadius / 2;
    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
        HiLog.info(Contants.LABEL, "onMeasure");
        int widthMode = EstimateSpec.getMode(widthMeasureSpec);
        int widthSize = EstimateSpec.getSize(widthMeasureSpec);
        int heightMode = EstimateSpec.getMode(heightMeasureSpec);
        int heightSize = EstimateSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == EstimateSpec.PRECISE) {
            width = widthSize;
        } else {
            width = (mSunRadius + mFixLineHeight + mLineHeight) * 2 + getPaddingRight() + getPaddingLeft();
        }
        if (heightMode == EstimateSpec.PRECISE) {
            height = heightSize;
        } else {
            height = (mSunRadius + mFixLineHeight + mLineHeight) * 2 + getPaddingTop() + getPaddingBottom();
        }
        setEstimatedSize(width, height);
        mWidth = width;
        mHeight = height;
        System.out.println("cccccccccc " + mWidth + " " + mHeight);
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
//        PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
//        opts.pixelFormat = PixelFormat.ARGB_8888;
//        opts.size = new Size(getWidth(), getHeight());
//        PixelMap pixelMap = PixelMap.create(opts);
//        PixelMapElement pixelMapElement = new PixelMapElement(pixelMap);
//        pixelMapElement.setFilterPixelMap(true);
        System.out.println("720--------onDraw");
        drawLines(canvas);
    }

    /**
     * 绘制line
     *
     * @param canvas 画布
     */
    private void drawLines(Canvas canvas) {
        for (int i = 0; i <= 360; i++) {
            if (i % mLineLevel == 0) {
                mLineLeft = mWidth / 2 - mLineWidth / 2;
                mLineTop = mHeight / 2 - mSunRadius - mFixLineHeight;
                mLineBottom = mLineTop + mLineHeight;

                canvas.save();
                canvas.rotate(i, mWidth / (float) 2, mHeight / (float) 2);
                canvas.drawLine(mLineLeft, mLineTop, mLineLeft, mLineBottom, mLinePaint);
                System.out.println("cccccccccc " + mWidth + "---" + mHeight + "---" + mLineLeft + "---" + mLineTop + "---" + mLineLeft + "---" + mLineBottom + " " + ((ComponentContainer) getComponentParent()).getTranslationY());
                canvas.restore();
            }
        }
    }

    public int changeDp(int value) {
        return AttrHelper.vp2px(value, getContext());
    }
}
